﻿using System.Configuration;
using System.Web.Configuration;
using Comsenz.UCenter.Client.API.DAL;
using Comsenz.UCenter.Client.API.DL;

namespace Comsenz.UCenter.Client.API
{
    /// <summary>
    /// 数据操作库助手
    /// </summary>
    /// <author>Chiron</author>
    internal static class RepositroyHelper
    {
        /// <summary>
        /// 取得数据环境
        /// </summary>
        public static UCenterDBDataContext GetUCenterDB()
        {
            var webConfig = WebConfigurationManager.OpenWebConfiguration("~/");
            var membershipSection = webConfig.GetSection("system.web/membership") as MembershipSection;
            var providerNode = membershipSection.Providers[membershipSection.DefaultProvider];
            var connName = providerNode.Parameters["connectionStringName"];
            ConnectionStringSettings conn = WebConfigurationManager.ConnectionStrings[connName];
            var connStr = conn.ConnectionString;
            return new UCenterDBDataContext(connStr);
        }

        /// <summary>
        /// 取得用户数据操作库
        /// </summary>
        /// <param name="db">数据环境</param>
        /// <returns></returns>
        public static UCenterMemberRepository GetUCenterMemberRepository(UCenterDBDataContext db = null)
        {
            if (db == null) db = GetUCenterDB();
            return new UCenterMemberRepository(db);
        }
    }
}
